//
//  CTVVideoPlayerController.m
//  Channltv
//
//  Created by Bruno Keymolen on 13/11/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVVideoPlayerController.h"
#import "CTVConfig.h"
#import "CTVCache.h"

#import "CTVNavigationManager.h"

@implementation CTVVideoPlayerController

-(id)initWithScene: (BRRenderScene *)scene andClip:(CTVClip*)clip{
	[super initWithScene:scene];
	
	m_clip = clip;
	[m_clip retain];
	
	m_playOtherChannel = NO;
	m_oldChannel = [[CTVNavigationManager GetInstance] currentChannel];

	if([[[NSUserDefaults standardUserDefaults] objectForKey:CTVCONF_CHANNELVIEW] isEqualToString:@"Yes"] ){
		m_playClipNext = YES;
	}else{
		m_playClipNext = NO;	
	}

	return self;
}

-(void)dealloc{
	[m_clip release];
	
	if(m_channelSwitchTimer){
		[m_channelSwitchTimer invalidate];
	}
	if(m_timer){
		[m_timer invalidate];
	}
	if(m_infoController){
		[m_infoController release];
	}
	[super dealloc];
}

- (BOOL)brEventAction:(BREvent*)event{
	CTVChannel* channel;
	BOOL ret = FALSE;

	//We are only interested in the key-down events.
	if([event value] == 0){
		return [super brEventAction:event];
	}

	if([[[NSUserDefaults standardUserDefaults] objectForKey:CTVCONF_CHANNELVIEW] isEqualToString:@"Yes"] ){
		switch([event remoteAction]){
			case kBRRemoteActionMenu:
				[(BRQTKitVideoPlayer*)[self videoPlayer] pause];
				ret = TRUE;
				m_timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(_removeScreenTimed:) userInfo:nil repeats:NO];
				[m_timer retain];
				
				m_playClipNext = NO;
				break;
			case kBRRemoteActionUp:
				NSLog(@"kBRRemoteActionUp -> [CTVChannelSwitchInfoController alloc]");
				channel = [[CTVNavigationManager GetInstance] nextChannel];
				[self showChannelSwitchContollerTimed];
			
				m_playClipNext = NO;
				ret = TRUE; //We handled the event
				break;
			case kBRRemoteActionDown:
				NSLog(@"kBRRemoteActionDown -> [CTVChannelSwitchInfoController alloc]");
				channel = [[CTVNavigationManager GetInstance] prevChannel];
				[self showChannelSwitchContollerTimed];
			
				m_playClipNext = NO;
				ret = TRUE; //We handled the event
				break;
		}
	}else{
		//Clip view mode
		ret = FALSE;
		m_playClipNext = NO;
	}
	
	if(ret == FALSE){
		ret = [super brEventAction:event];
	}
	
	
	return ret;
}

-(BOOL)playOtherChannel{
	return m_playOtherChannel;
}


-(BOOL)playClipNext{
	return m_playClipNext;
}


- (void)wasPushed{
	[super wasPushed];

	//Keep the position in the channel
	CTVChannel* channel = [m_clip pChannel];
	[[channel watchPoint] setClipID:[m_clip clipID]]; 
//	[[channel watchPoint] setOffset:[[self videoPlayer] elapsedPlaybackTime]];

//NSLog(@"ALLOC - GET; elapsedPlaybackTime; [[m_videoPlayerController videoPlayer] elapsedPlaybackTime] = %f \n channel=%@", [[self videoPlayer] elapsedPlaybackTime], [channel name] );

}


-(void)willBePopped{
	[super willBePopped];

	//Keep the position in the channel
	CTVChannel* channel = [m_clip pChannel];
	[[channel watchPoint] setClipID:[m_clip clipID]]; 
	[[channel watchPoint] setOffset:[[self videoPlayer] elapsedPlaybackTime]];

NSLog(@"DEALLOC - GET; elapsedPlaybackTime; [[m_videoPlayerController videoPlayer] elapsedPlaybackTime] = %f \n channel=%@", [[self videoPlayer] elapsedPlaybackTime], [channel name] );
    
	//Write the watchpoint to persistent memory
	[[CTVCache GetInstance] saveWatchPoint:channel];				
}



- (void)willBeBuried{
	[(BRQTKitVideoPlayer*)[self videoPlayer] resume];	
}


- (void)wasBuriedByPushingController:(id)controller{
	[super wasBuriedByPushingController:controller];
}


-(void)wasExhumedByPoppingController:(id)controller{
	if(controller == m_infoController){
		NSLog(@"Old Channel:%@ \nNavigation Manager Current Channel:%@", [m_oldChannel name], [[[CTVNavigationManager GetInstance] currentChannel] name]);
		//Check which channel is selected, 
		if([[m_oldChannel channelID] isEqualToString:[[[CTVNavigationManager GetInstance] currentChannel] channelID]]){
			//Same channel, just continue
			//[(BRQTKitVideoPlayer*)[self videoPlayer] resume];
		}else{
			//Other channel, goto that channel
			NSLog(@"Channel switch -> %@", [[[CTVNavigationManager GetInstance] currentChannel] name] );
			if(m_timer){[m_timer invalidate];};
			m_timer = [NSTimer scheduledTimerWithTimeInterval:0.25f target:self selector:@selector(_removeScreenTimed:) userInfo:nil repeats:NO];
			[m_timer retain];
			m_playOtherChannel = TRUE;
		}
		
		//Store the oldChannel
		m_oldChannel = [[CTVNavigationManager GetInstance] currentChannel];
		
	}
		
	[super wasExhumedByPoppingController:controller];	
}


-(void)setDelegate:(id)fp8{
	[super setDelegate:fp8];
}


-(void)showChannelSwitchContollerTimed{
	
[self _showChannelSwitchContollerTimed:nil];
//	if(m_channelSwitchTimer)[m_channelSwitchTimer invalidate];
//	m_channelSwitchTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(_showChannelSwitchContollerTimed:) userInfo:nil repeats:NO];
//	[m_channelSwitchTimer retain];
}

-(void)_showChannelSwitchContollerTimed:(NSTimer*)timer{
	if(m_infoController != nil){
		[m_infoController release];
	}
	CTVChannel* channel = [[CTVNavigationManager GetInstance] currentChannel];
	m_infoController = [[CTVChannelSwitchInfoController alloc] initWithScene:[self scene] andChannel:channel];
	[[self stack] pushController:m_infoController];
}

-(void)_handlePlaybackError:(id)fp8{
	//[super _handlePlaybackError:fp8];
	m_playClipNext = YES;
	[[self stack] removeController:self];	
}


-(void)_removeScreenTimed:(NSTimer*)timer{
	[[self stack] removeController:self];	
}

@end
